﻿CREATE PROCEDURE [dbo].[vk_GetPartners]
	@userId NVARCHAR(128),
	@partners NVARCHAR(MAX)
AS
BEGIN
	SET @userId = LTRIM(RTRIM(LOWER(@userId)))
	SET @partners = LTRIM(RTRIM(LOWER(@partners)))

	DECLARE @ids TABLE
	(
		[id] int,
		[value] nvarchar(128)
	)

	INSERT INTO @ids
	SELECT * FROM [dbo].[SplitStringByDelimiter](@partners, N'###')

	SELECT [PartnerId]
      ,[Complete]
	FROM [dbo].[vk_Relations]
	WHERE [UserId] = @userId AND [PartnerId] IN (SELECT [value] FROM @ids) AND [Approve] = 0


	SELECT DISTINCT COUNT(*) as 'Count'
	FROM [dbo].[vk_Relations] AS r1
	INNER JOIN [dbo].[vk_Relations] AS r2 ON r1.[UserId] = r2.[PartnerId]
	WHERE r1.[UserId] = @userId AND r1.[PartnerId] = r2.[UserId] AND r1.[Complete] = 1 AND r2.[Complete] = 1 AND r1.[Approve] = 0 --AND r2.[Approve] = 0


END